package com.johnny.myRedis;

import redis.clients.jedis.Jedis;

import java.util.*;

public class RedisAPITest {

    Jedis jedis;

    public RedisAPITest() {
        this.jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
        this.jedis.auth("654321");
    }

    public void getKeys() {
        Set<String> keys = this.jedis.keys("*");
        for (Iterator iterator = keys.iterator(); iterator.hasNext(); ) {
            String key = (String) iterator.next();
            System.out.println(key);
        }
        System.out.println("jedis.exists=====>" + this.jedis.exists("k2"));
        System.out.println(this.jedis.ttl("k1"));  //查看过期时间
        closeRedis();
    }

    public void getStringTest() {
        // this.jedis.append("k1", "myreids"); //拼接字符串
        System.out.println(this.jedis.get("k1"));
        this.jedis.set("k4", "k4_reids");
        System.out.println("-------------------------------");
        this.jedis.mset("str1", "v1", "str2", "v2", "str3", "v3", "str4", "v4");//设置多个
        System.out.println(this.jedis.mget("str1", "str2", "str3", "str4"));
        closeRedis();
    }

    public void getListTest() {
        // this.jedis.lpush("mylist","v1","v2","v3","v4","v5");
        List<String> list = this.jedis.lrange("mylist", 0, -1);
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
        closeRedis();
    }

    public void getSetTest() {
//        this.jedis.sadd("orders", "jd001");
//        this.jedis.sadd("orders", "jd002");
//        this.jedis.sadd("orders", "jd003");
        Set<String> set1 = this.jedis.smembers("orders");
        for (Iterator iterator = set1.iterator(); iterator.hasNext(); ) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
        this.jedis.srem("orders", "jd002"); //删除
        System.out.println(this.jedis.smembers("orders").size());
        closeRedis();
    }

    public void getHashTest() {
        this.jedis.hset("hash1", "userName", "lisi");
        System.out.println(this.jedis.hget("hash1", "userName"));
        Map<String, String> map = new HashMap<String, String>();
        map.put("telphone", "12345678989");
        map.put("address", "guihu");
        map.put("email", "abc@163.com");
        this.jedis.hmset("hash2", map);
        List<String> result = this.jedis.hmget("hash2", "telphone", "email");
        for (String element : result) {
            System.out.println(element);
        }
        closeRedis();
    }

    public void getZsetTest() {
        this.jedis.zadd("zset01", 60d, "v1");
        this.jedis.zadd("zset01", 70d, "v2");
        this.jedis.zadd("zset01", 80d, "v3");
        this.jedis.zadd("zset01", 90d, "v4");

        Set<String> s1 = this.jedis.zrange("zset01", 0, -1);
        for (Iterator iterator = s1.iterator(); iterator.hasNext(); ) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
        closeRedis();
    }

    public void closeRedis() {
        JedisPoolUtil.release(this.jedis);
    }

    public static void main(String[] args) {
        RedisAPITest rat = new RedisAPITest();
        //rat.getKeys();
        //rat.StringTest();
        //rat.getListTest();
        //rat.getSetTest();
        //rat.getHashTest();
        rat.getZsetTest();
    }

}
